const http = require('http');
const fs = require('fs');
http.createServer((request, response) => {
    const {url, method, headers} = request;
    if (url === '/' && method === 'GET'){
        fs.readFile('index.html', (err, data) =>{
            if(err){
                response.writeHead(500, {
                    'Content-Type':'text/plain;charset=UTF-8',
                });
                response.end('500 服务器挂了!!!!');
                return
            }
            response.statusCode = 200;
            response.setHeader('Content-Type','text/html');
            response.end(data);
        })
    }else if(url === '/users1' && method  === 'GET'){
        response.writeHead(200, {'Content-Type':'application/json'});
        response.end(JSON.stringify({name:'tom'}));
    } else if (method === "GET" && headers.accept.indexOf("image/*" !== -1)) {
        // 拿到所有图片
        // 直接使用readFile读取 不行， 因为它要把全部图片内容加载到服务器
        // 使用 流 stream ！！！！
        fs.createReadStream("." + url.pipe(request));
    } else {
        response.statusCode = 404
        response.setHeader('Content-Type','text/plain;charset=UTF-8');
        response.end('404 没这玩意');
    }
}).listen(3000, () =>{
    console.log('Server at 3000');
});
function getPrototypeChain(obj){
    const protoChain = [];
    while(obj = Object.getPrototypeOf(obj)){
        protoChain.push(obj);
    }
    return protoChain;
}